//
//  LGTNickNameVC.swift
//  KJLGT
//
//  Created by jenkin on 2019/9/2.
//  Copyright © 2019年 jenkin. All rights reserved.
//

import UIKit
import WisdomHUD

class LGTNickNameVC: LGTBaseVC {

    var nickName:String = ""
    var textView:LGTTextField?
    
    override func viewDidLoad() {
        super.viewDidLoad()

        // Do any additional setup after loading the view.
    }
    
    override func setUI() {
        super.setUI()
        self.view.backgroundColor = ViewBackColor
        self.setBavBarStyle(navBarHiden: false, title: "昵称", back: true)
        self.textView = LGTViewCreateUtil.share.createLGTTextFied(font: Font_Normal_18, keyBoardType: UIKeyboardType.default, placeholder: "为自己取个别致的昵称吧~", textColor: TextColor, limitCout: 6)
        self.textView?.leftView = UIView.init(frame: CGRect.init(x: 0, y: 0, width: SizeFit(12), height: 0))
        self.textView?.text = self.nickName
        self.textView?.backgroundColor = UIColor.white
        self.textView?.leftViewMode = UITextField.ViewMode.always
        self.view.addSubview(self.textView!)
        self.textView?.snp.makeConstraints({ (make) in
            make.left.right.equalTo(0)
            make.top.equalTo(NavBarH() + SizeFit(10))
            make.height.equalTo(SizeFit(50))
        })
        
        let rightButton:LGTButton = LGTViewCreateUtil.share.createTextLGTButton(title: "保存", font: Font_Normal_16, color: TextColor, selectedColor: TextColor)
        self.navBar.addSubview(rightButton)
        rightButton.backgroundColor = UIColor.clear
        rightButton.snp.makeConstraints { (make) in
            make.right.equalTo(-SizeFit(10))
            make.width.equalTo(SizeFit(60))
            make.height.equalTo(40)
            make.bottom.equalTo(0)
        }
        rightButton.addTarget(self, action: #selector(saveAction), for: UIControl.Event.touchUpInside)
    }

    @objc func saveAction(){
        
        if self.textView?.text?.count == 0{
            WisdomHUD.showError(text: "请为自己取个别致的昵称吧~")
            return
        }
        
        if LGTOCBaseUtil.isContainsEmoji(self.textView?.text ?? "") == true{
            WisdomHUD.showError(text: "请不要输入表情符号")
            return
        }
        
        let nickName:String = self.textView?.text ?? ""
        let param = ["nickname": nickName]
        self.updateUserInfo(dict: param, showMessage: true)
    }
    
    func updateUserInfo(dict:Dictionary<String, Any>, showMessage:Bool = true){
        weak var weakSelf = self
        LGTHttpUtil.httpUtil.requestData(url: User_Info_Update_URL, param: dict, showMessage: showMessage, success: { (json) in
            weakSelf?.navigationController?.popViewController(animated: true)
        }) { (json) in
            
        }
    }
}
